package wsepForum.ui.jaxcent.states;

import java.util.Collection;
import java.util.Vector;

import jaxcent.HtmlElement;
import jaxcent.HtmlOption;
import jaxcent.HtmlSelect;
import jaxcent.Jaxception;
import wsepForum.common.elements.Section;
import wsepForum.domain.handlers.AUserHandler;
import wsepForum.ui.jaxcent.IXContext;

/**
 * @author Oren
 *
 */
public class XSectionEdit extends AXSectionControl {

	/*****************************
	 * MEMBERS                   *
	 *****************************/
	private HtmlSelect xModeratorsToRemove;

	/*****************************
	 * CONSTRUCTOR               *
	 *****************************/
	public XSectionEdit(IXContext context, Section section) {
			super(context,section);
	}


	/*****************************
	 * METHODS                   *
	 *****************************/
	/* (non-Javadoc)
	 * @see wsepForum.ui.jaxcent.states.AXSectionControl#dealWithSubmit(java.lang.String, java.util.Collection)
	 */
	protected void dealWithSubmit(String userName, Collection<String>newModerators) throws Jaxception {
		try {
			HtmlOption option;
			Vector <String> delModerators = new Vector<String>();
			// gather information about moderators to remove
			for (int i=0, size=this.xModeratorsToRemove.getLength();i<size;i++) {
				option = this.xModeratorsToRemove.getOption(i);
				if (option.getSelected()) {
					delModerators.add(option.getText());
				}
			}
			if (newModerators.size()==0&delModerators.size()==0) {
				this.context.getPage().showMessageDialog("You must select users to add or remove from this section as moderators!");
				return;
			}
			int sectionID = this.section.getElementID();
			AUserHandler handler = this.context.getSession().getHandler();
			Section updatedSection = this.section;
			// doing the real action
			for (String toAdd : newModerators) {
				updatedSection = handler.addModeratorToSection(sectionID, toAdd);
			}
			for (String toRemove : delModerators) {
				updatedSection = handler.removeModerator(sectionID, toRemove);
			}
			this.context.loadSection(updatedSection);
		} catch (Exception e) {
			this.context.getPage().showMessageDialog("Problem with changing section's moderator, possible that someone\n" +
					"else changed the moderator's list, try realoding the page\n"+e.getMessage());
		}
	}

	/* (non-Javadoc)
	 * @see wsepForum.ui.jaxcent.states.AXSectionControl#initContent()
	 */
	protected void initContent() {
		super.initContent();
		try {
			this.xModeratorsToRemove = new HtmlSelect(this.context.getPage(),"xmoderatorsremove" );
			Vector<String> users = this.context.getSession().getHandler().getManagedUsernames();
			Vector<String> moderators = this.section.getAllSectionMods();
			// adds (non moderators) users to moderators list
			for (int i=0;i<users.size();i++) {
				String user = users.elementAt(i);
				if (!moderators.contains(user)) {
					this.xModerators.insertOption(i, user);
				}
			}
			// adds all section's moderators to "remove mods" list
			for (int i=0;i<moderators.size();i++) {
				String user = moderators.elementAt(i);
				this.xModeratorsToRemove.insertOption(i, user);
			}
			if (moderators.size()> 0) {
				new HtmlElement(this.context.getPage(),"xremovefield").show();
			}
			this.xTitle.setInnerHTML("Edit section");
			this.xSubmit.setValue("Edit");
		} catch (Jaxception e) {
			this.context.getPage().showMessageDialog("Cannot put data on page's field, please try reloading the page.\n"+e.getMessage());
		}
	}

	/* (non-Javadoc)
	 * @see wsepForum.ui.jaxcent.states.AXSectionControl#construct()
	 */
	@Override
	protected void construct() {
		construct(
				"<strong>"+this.section.getHeadLine()+"</strong>",
				"<dl style=\"display: none;\" id=\"xremovefield\"><dt><label for=\"moderators\">Remove Moderators:</label><br /><span>Select users that not longer will serve as moderators for this section.</span></dt><dd>"+
				"<select tabindex=\"3\" name=\"xmoderatorsremove[]\" id=\"xmoderatorsremove\" multiple=\"multiple\" size=\"10\"></select></dd>" +
				"</dl>"
		);
	}
}
